<template>
  <div>
      <el-button @click="writer">写入</el-button>
      <input type="file" id="files" @click="writer">
      <input type="button" id="export" value="保存">
  </div>
</template>

<script>
// import e from 'https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.js'
import area from '@/assets/area.js'
export default {
  name:'doc',
  data(){
    return{
      area:area,
      data:[]
    }
  },
  methods:{
   writer(){
      // console.log(area)
      var writeFiles = document.getElementById('files')
      // console.log(writeFiles)
    writeFiles.addEventListener('change',function(){
          var files1 = writeFiles.files[0]
          // console.log(files1)  // => 图一
          var reader = new FileReader()
          reader.readAsText(files1)
          reader.onload = function() {
            // console.log(this.result) // => 图二
            let data = JSON.parse(this.result)
            // console.log(data) // => 图三
            data.forEach(item => {
              item.children.forEach(dis => {
                // console.log(dis)
                dis.children.unshift({
                  label:`全${dis.label}`,
                  value:dis.value
                })
              })
            })
            console.log(JSON.stringify(data)) // => 图四
            this.data = data
            // console.log(this.data)
          }
      },false)
      var button = document.getElementById('export')
      button.addEventListener('click',function() {
        var files1 = writeFiles.files[0]
        var reader = new FileReader()
        reader.readAsText(files1)
        reader.onload = function() {
            // console.log(this.result) // => 图一
            var data1 = JSON.parse(this.result)
            // console.log(data) // => 图二
            data1.forEach(item => {
              item.children.forEach(dis => {
                // console.log(dis)
                dis.children.unshift({
                  label:`全${dis.label}`,
                  value:dis.value
                })
              })
            })
            console.log(data1)
            console.log(JSON.stringify(data1))
            let data1JSON = JSON.stringify(data1)
            var blob = new Blob([data1JSON],{type:"text/plain;charset=utf-8"})
            saveAs(blob,"save json")
        }
      },false)
    }
  }
}
</script>

<style>

</style>